package com.breaktian.assemble.customviews.tableview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;


import java.util.ArrayList;
import java.util.List;

/**
 * Created by keke.tian on 2018/2/7.
 */

public class TableView extends TableLayout {


    private Context mContext;
    private TableAdapter mTableAdapter;

    public TableView(Context context) {
        super(context);
        init(context);
    }

    public TableView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        this.mContext = context;

    }

    public void init(TableAdapter tableAdapter,int columnCount,List<? extends Object> list,TableAdapter.ItemClickListener itemClickListener){
        this.mTableAdapter = tableAdapter;
        tableAdapter.setOnItemClick(itemClickListener);
        tableAdapter.initLayout(columnCount,this,list,mContext);
    }

    /**
     * 折叠保留行数
     * */
    public void foldRemainRows(int remainRows){
        mTableAdapter.foldRemainRows(remainRows);
    }

    /**
     * 展开折叠
     * */
    public void unfold(){
        mTableAdapter.unfold();
    }


    /**
     * 表格数据适配器
     * TableAdapter
     * */
    public abstract static class TableAdapter {

        private Context mContext;
        private List<? extends Object> mItems;
        private TableLayout mLayout;
        private int mColumnCount;
        private ItemClickListener mListener;
        private List<TableRow> mTableRows = new ArrayList<>();
        private List<View> mItemViews = new ArrayList<View>();


        public TableAdapter() {
        }

        public void foldRemainRows(int remainRows){
            int size = mTableRows.size();
            for (int i=0;i<size;i++){
                TableRow row = mTableRows.get(i);
                if(i<remainRows){
                    row.setVisibility(VISIBLE);
                }else{
                    row.setVisibility(GONE);
                }
            }
        }

        public void unfold(){
            int size = mTableRows.size();
            for (int i=0;i<size;i++){
                TableRow row = mTableRows.get(i);
                row.setVisibility(VISIBLE);
            }
        }

        public List<? extends Object> getData(){
            return mItems;
        }

        public List<View> getViews() {
            return mItemViews;
        }

        public void addViews(View v) {
            mItemViews.add(v);
        }

        private void initLayout(int columnCount, TableLayout tableLayout, List<? extends Object> items,
                                Context context) {
            mContext = context;
            mItems = items;
            mLayout = tableLayout;
            mColumnCount = columnCount;
            if (mItems == null||mContext==null||mLayout==null||mColumnCount<1)
                return;

            refreshLayout(mColumnCount, mLayout, mItems, mContext);
        }

        private void refreshLayout(int columnCount, TableLayout layout, List<? extends Object> items,
                                Context context) {
            layout.removeAllViews();
            mTableRows.clear();
            mItemViews.clear();
            int count = items.size();

            int rowCount = count / columnCount;
            if (count % columnCount != 0)
                rowCount = rowCount + 1;

            TableRow row = null;
            ViewGroup.LayoutParams rowParam = null;
            for (int i = 0; i < rowCount * columnCount; i++) {
                TableRow.LayoutParams itemParam = new TableRow.LayoutParams(0,
                        TableRow.LayoutParams.WRAP_CONTENT);
                itemParam.weight = 1;

                if (i % columnCount == 0) {
                    row = new TableRow(context);
                    rowParam = new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                            ViewGroup.LayoutParams.WRAP_CONTENT);
                    layout.addView(row, rowParam);
                    mTableRows.add(row);
                }

                View itemView = null;

                if (i >= count) {
                    itemView = new TextView(context);
                    itemView.setVisibility(View.INVISIBLE);
                } else {
                    final Object itemObj = mItems.get(i);
                    itemView = getItemView(context, itemObj, i);
                    mItemViews.add(itemView);
                    //回调监听方法
                    final View itemView2 = itemView;
                    final int position = i;
                    itemView.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if(mListener!=null) mListener.onClick(itemObj,position,itemView2);
                        }
                    });
                    itemView.setOnLongClickListener(new OnLongClickListener() {
                        @Override
                        public boolean onLongClick(View view) {
                            if(mListener!=null) mListener.onLongClick(itemObj,position,itemView2);
                            return true;
                        }
                    });
                }
                row.addView(itemView, itemParam);
            }
        }

        /**
         * 抽象方法 业务在此处写
         */
        public abstract View getItemView(final Context context, final Object itemObj, final int position);

        public void setOnItemClick(ItemClickListener listener) {
            this.mListener = listener;
        };

        /**
         * click监听
         * */
        public interface ItemClickListener {
            void onClick(Object o, int pos, View v);

            void onLongClick(Object o, int pos, View v);
        }


    }





}
